package com.hand.wfive.util

import android.app.Dialog
import android.view.Gravity
import android.view.WindowManager
import android.widget.SeekBar
import androidx.databinding.adapters.SeekBarBindingAdapter.setOnSeekBarChangeListener
import androidx.viewpager.widget.ViewPager
import com.hand.wfive.R

inline fun SeekBar.seekBarChange(crossinline block: (SeekBar, Int, Boolean) -> Unit) {
    setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
        override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
            block(seekBar, progress, fromUser)
        }
    })
}

inline fun ViewPager.pageChange(crossinline block: (Int) -> Unit) {
    addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) = Unit
        override fun onPageScrolled(
            position: Int, positionOffset: Float, positionOffsetPixels: Int
        ) = Unit

        override fun onPageSelected(position: Int) {
            block(position)
        }
    })
}

fun Dialog.bottomShow(show: Boolean = true) {
    window?.setGravity(Gravity.BOTTOM)
    window?.setWindowAnimations(R.style.DialogAnimBottom)
    window?.setLayout(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.WRAP_CONTENT
    )
    if (show) show()
}